﻿using BLL;
using DBUtility;
using EncryptHelper;
using gfLibrary;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal
{
    public partial class Add : System.Web.UI.Page
    {
        private string Pass { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetModel();
            }
        }

        protected void GetModel()
        {
            CONST_DEFINE_BLL bll = new CONST_DEFINE_BLL();
            var role = bll.GetModelList("CON_CHNAME='ZNYHGW'");
            ListItem items;
            foreach (var r in role)
            {
                items = new ListItem();
                items.Text = r.CON_CON;
                items.Value = r.CON_CODE;
                chbELicense.Items.Add(items);
            }
        }

        protected void txtEId_TextChanged(object sender, EventArgs e)
        { 

        }

        protected void btnSaveImg_Click(object sender, EventArgs e)
        {
            string strNo = txtENo.Text.Trim();
            if (string.IsNullOrEmpty(strNo))
            {
                txtENo.Focus();
                return;
            }

            string strId = txtEId.Text.Trim();
            if (string.IsNullOrEmpty(strId))
            {
                txtEId.Focus();
                return;
            }

            string strPwd = txtEPwd.Text.Trim();
            if (strPwd.Trim().Length == 0)
            {
                txtEPwd.Focus();
                return;
            }

            string strPwdC = txtEPwdC.Text.Trim();
            if (strPwd != strPwdC)
            {
                txtEPwd.Focus();
                return;
            }

            string strName = txtEName.Text.Trim();
            if (strName.Trim().Length == 0)
            {
                txtEName.Focus();
                return;
            }

            EMPLOYEE_USER_BLL bll = new EMPLOYEE_USER_BLL();

            EMPLOYEE_USER[] users = bll.GetModelList("EMPLOYEE_ID='" + txtEId.Text.Trim() + "'");

            if (users.Length > 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "当前用户名已使用,请输入其他用户名！");
                txtEId.Focus();
            }


            DbFactory m_df = new DbFactory();

            EMPLOYEE_USER_BLL user = new EMPLOYEE_USER_BLL();
            EMPLOYEE_USER theone = new EMPLOYEE_USER();
            theone.EMPLOYEE_NO = txtENo.Text;
            theone.EMPLOYEE_ID = txtEId.Text;
            theone.EMPLOYEE_NAME = txtEName.Text;
            theone.LICENSE_ID = txtEId.Text;
            theone.EMPLOYEE_PWD = Encrypt.MD5Encrypt(txtEId.Text.Trim().ToLower() + txtEPwdC.Text.Trim().ToLower());

            //POWER
            theone.EMPLOYEE_POWER = drpEPower.SelectedItem.Value;
            //REG_DATE
            theone.REG_DATE = DateTime.Now.ToString();
            //LICENSE
            string selectValue = "";
            int mark = 0;
            for (int i = 0; i < chbELicense.Items.Count; i++)
            {
                if (chbELicense.Items[i].Selected)
                {
                    selectValue += chbELicense.Items[i].Value + ",";
                    mark++;
                }
            }
            if (mark==0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "用户权限不能设置为空!!");
                return;
            }
            theone.LICENSE_NAME = selectValue;

            m_df.Add(user.InsertSQL(theone));
            if (m_df.Execute() == true)
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "添加用户成功!");
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "xxxe", "window.location.href='Admin.aspx';", true);
            }
            else
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "添加用户失败!");
                
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin.aspx");
        }
    }
}